package com.es.action.heat.filechange;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.es.util.file.ConfigUtil;
import com.es.util.file.FileUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件监控
 *
 * @author Will
 */
public class FileMonitor {

    public static Map<String, Long> getAllFileState() {

        HashMap<String, Long> map = new HashMap<>();

        Object monitorFile = ConfigUtil.getDefaultFromProperties("monitorFile");
        if (monitorFile == null) {
            System.err.println("监测文件为空");
            return map;
        }

        String s = monitorFile.toString();

        HashMap<String, Long> fileModified = new HashMap<>();

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<FilePath> filePaths = objectMapper.readValue(s, new TypeReference<List<FilePath>>() {
            });
            filePaths.forEach(x -> fileModified.putAll(FileUtil.getFileModified4Java(FileUtil.replaceName(x.getPath()))));
        } catch (JsonProcessingException e) {
            System.err.println(e.getMessage());
        }

        return fileModified;
    }

}
